<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"MS-8");
$currentPage = $_SERVER["PHP_SELF"];

mysql_select_db($database_con_gl, $con_gl);
$query_l_kel = "SELECT id, area FROM gl_area";
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_l_kel .= " WHERE id='$_SESSION[area]'";
	$_GET['cr_kel'] = $_SESSION['area'];
}

$query_l_kel .=" ORDER BY area ASC"; //echo $query_l_kel;
$l_kel = mysql_query($query_l_kel, $con_gl) or die(mysql_error());
$row_l_kel = mysql_fetch_assoc($l_kel);
$totalRows_l_kel = mysql_num_rows($l_kel);

mysql_select_db($database_con_gl, $con_gl);
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_l_kel1 = "SELECT id, gudang FROM gl_gudang WHERE area = '$_GET[cr_kel]' and id='$_SESSION[lokasi]' ORDER BY gudang ASC";
	$_GET[cr_kel1] = $_SESSION['lokasi'];
}else{
	//if($_GET['cr_kel']!=""){
		$query_l_kel1 = "SELECT id, gudang FROM gl_gudang WHERE area = '$_GET[cr_kel]' ORDER BY gudang ASC";
	//}
} //echo $query_l_kel1;
$l_kel1 = mysql_query($query_l_kel1, $con_gl) or die(mysql_error());
//$row_l_kel1 = mysql_fetch_assoc($l_kel1);
$totalRows_l_kel1 = mysql_num_rows($l_kel1);

/* Edited by suwondo */
$maxRows_data = 20;
$pageNum_data = 0;
if (isset($_GET['pageNum_data'])) {
  $pageNum_data = $_GET['pageNum_data'];
}
$startRow_data = $pageNum_data * $maxRows_data;

$cr_txt = str_replace("+"," ",$_REQUEST[cr_txt]);
mysql_select_db($database_con_gl, $con_gl);
$query_data = "SELECT gl_masterkendaraan.id, gl_masterkendaraan.nopolisi, gl_masterkendaraan.kodeasset, gl_merkkendaraan.merk, gl_tipekendaraan.tipekendaraan, gl_jeniskendaraan.jeniskendaraan, gl_masterkendaraan.tahun, gl_area.area, gl_gudang.gudang, gl_statuskendaraan.status,
(CASE when (gl_masterkendaraan.unitstok = '1') THEN 'Corporate' when (gl_masterkendaraan.unitstok = '2') THEN 'Retail' when (gl_masterkendaraan.unitstok = '3') THEN 'Ops / Tugas Kantor' when (gl_masterkendaraan.unitstok = '4') THEN 'UGC' end )AS 'statusunit', 
(CASE when (gl_masterkendaraan.tersedia = '1') THEN 'AVAILABLE' ELSE 'NOT AVAILABLE' end) AS 'ketersediaan' from gl_masterkendaraan, gl_merkkendaraan, gl_tipekendaraan, gl_jeniskendaraan, gl_area, gl_gudang, gl_statuskendaraan WHERE gl_merkkendaraan.id = gl_masterkendaraan.merk AND gl_tipekendaraan.id = gl_masterkendaraan.tipekendaraan AND gl_jeniskendaraan.id = gl_masterkendaraan.jeniskendaraan AND gl_area.id = gl_masterkendaraan.area AND gl_gudang.id = gl_masterkendaraan.cabang AND gl_statuskendaraan.id = gl_masterkendaraan.status";
if(isset($_GET[cr_txt]) AND $_GET[cr_txt]<>'' AND $_GET[cr_txt]<>'+') { 
$decode_cr = str_replace("+"," ",$_GET[cr_txt]);
$query_data .=" AND (gl_statuskendaraan.status LIKE '%%$decode_cr%%' OR gl_masterkendaraan.nopolisi LIKE '%%$decode_cr%%' OR gl_masterkendaraan.kodeasset LIKE '%%$decode_cr%%' OR gl_merkkendaraan.merk LIKE '%%$decode_cr%%' OR gl_tipekendaraan.tipekendaraan LIKE '%%$decode_cr%%' OR gl_jeniskendaraan.jeniskendaraan LIKE '%%$decode_cr%%')";
}
if(isset($_GET[cr_kel]) AND $_GET[cr_kel]<>'ALL') { 
$query_data .=" AND gl_masterkendaraan.area ='$_GET[cr_kel]'";
}
if(isset($_GET[cr_unit]) AND $_GET[cr_unit]<>'ALL') { 
$query_data .=" AND gl_masterkendaraan.unitstok ='$_GET[cr_unit]'";
}
if(isset($_GET[tahun]) AND $_GET[tahun]<>'0') { 
$query_data .=" AND gl_masterkendaraan.tahun ='$_GET[tahun]'";
}
if(isset($_GET[cr_kel1]) AND $_GET[cr_kel1]<>'ALL') { 
$query_data .=" AND gl_masterkendaraan.cabang ='$_GET[cr_kel1]'";
}
if(!strstr($_SESSION[akses],'A-1-4')) {	
	$query_data .= " AND gl_gudang.id = '$_SESSION[lokasi]' ";
}
$query_data .=" ORDER BY gl_area.area, gl_masterkendaraan.nopolisi ASC"; 

$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRows_data = mysql_num_rows($data);

$totalPages_data = floor($totalRows_data / $maxRows_data);

$query_data .=" LIMIT $startRow_data, $maxRows_data"; //echo $query_data;
$data = mysql_query($query_data, $con_gl) or die(mysql_error());
$totalRow_data = mysql_num_rows($data);
$row_data = mysql_fetch_assoc($data);

$queryString_data = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_data") == false && 
        stristr($param, "totalRows_data") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_data = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_data = sprintf("&totalRows_data=%d%s", $totalRows_data, $queryString_data);
include('include/widget_exportdata.php');
?>
<script>
function viewImg(menuid) {
	$(document).ready(function(){
	  //$("#buttonpopUp" + grupid).click(function(){
		$("#popUp" + menuid).toggle(1000);
	  });
	//});
}
</script>

<h1>Data Master Stok Kendaraan</h1>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable"> 
  <tr valign="top" bgcolor="#FFFFFF" class="hide">
        <td><?php if(strstr($_SESSION[akses],"MS-8-1")) { ?><a href="index.php?component=masterkendaraan&task=add"><img src="images/add_new.png" border="0" /></a><?php } ?></td>
        <td colspan="8" align="right" style="border-bottom:solid 1px #E2EAC1;border-right:solid 1px #E2EAC1;"><form id="search" name="search" method="post" action="">
      <!-- <label>
      <select name="cr_kel" id="cr_kel" onchange="javascript: if(this.value=='ALL'){ cr_txt.value='Find by: status,nopolisi,merk,tipe,jenis,cabang'; }else{ cr_txt.value=''; cr_txt.focus(); }">
        <option value="ALL" <?php /*if (!(strcmp("ALL", $_GET[cr_kel]))) {echo "selected=\"selected\"";} */ ?>>Pilih Status Kendaraan</option>
        <?php
		/*do {  
		?>
				<option value="<?php echo $row_l_kel['id']?>"<?php if (!(strcmp($row_l_kel['id'], $_GET[cr_kel]))) {echo "selected=\"selected\"";} ?>><?php echo $row_l_kel['status']?></option>
				
				<?php
		} while ($row_l_kel = mysql_fetch_assoc($l_kel));
		  $rows = mysql_num_rows($l_kel);
		  if($rows > 0) {
			  mysql_data_seek($l_kel, 0);
			  $row_l_kel = mysql_fetch_assoc($l_kel);
		  }*/
		?>
      </select>
      </label> -->
      <select name="cr_kel" id="cr_kel" style="width:180px;" onchange="javascript: window.location.href='index.php?component=masterkendaraan&cr_kel='+this.value;">
        <option value="ALL" <?php if (!(strcmp("ALL", $_GET[cr_kel]))) {echo "selected=\"selected\"";} ?>>Pilih Area Ops</option>
        <?php
do {  
?>
        <option value="<?php echo $row_l_kel['id']?>"<?php if (!(strcmp($row_l_kel['id'], $_GET[cr_kel]))) {echo "selected=\"selected\"";} ?>><?php echo $row_l_kel['area']?></option>
        <?php
} while ($row_l_kel = mysql_fetch_assoc($l_kel));
  $rows = mysql_num_rows($l_kel);
  if($rows > 0) {
      mysql_data_seek($l_kel, 0);
	  $row_l_kel = mysql_fetch_assoc($l_kel);
  }
?>
      </select>
      <select name="cr_kel1" id="cr_kel1" style="width:180px;" onchange="javascript: window.location.href='index.php?component=masterkendaraan&cr_kel='+document.getElementById('cr_kel').value+'&cr_kel1='+this.value;">
        <option value="ALL" <?php if (!(strcmp("ALL", $_GET[cr_kel1]))) {echo "selected=\"selected\"";} ?>>Pilih Cabang</option>
        <?php
		while ($row_l_kel1 = mysql_fetch_assoc($l_kel1)){
		?>
        <option value="<?php echo $row_l_kel1['id']?>"<?php if (!(strcmp($row_l_kel1['id'], $_GET[cr_kel1]))) {echo "selected=\"selected\"";} ?>><?php echo $row_l_kel1['gudang']?></option>
        <?php
		} 
		?>
      </select>
      <select name="cr_unit" id="cr_unit" style="width:180px;" onchange="javascript: window.location.href='index.php?component=masterkendaraan&amp;cr_unit='+document.getElementById('cr_unit').value+'&amp;cr_unit='+this.value;">
        <option value="ALL" <?php if (!(strcmp("ALL", $_GET[cr_unit]))) {echo "selected=\"selected\"";} ?>>Pilih Unit</option>
        <option value="1">Corporate</option>
        <option value="2">Retail</option>
        <option value="3">Tugas Kantor</option>
        <option value="4">Ops / UGC</option>        
      </select>
      <select name="tahun" id="tahun" onchange="javascript: window.location.href='index.php?component=masterkendaraan&amp;tahun='+document.getElementById('tahun').value+'&amp;tahun='+this.value;">>
        <option value="0">--Pilih Tahun--</option>
				<?php
            $now = date("Y");
            for($i=$now; $i>$now-17; $i--){
                echo "<option value='$i'>$i</option>";
            }
            ?>
      </select>
     
	<!-- Edit by suwondo -->
	
	<label>
	<input name="cr_txt" type="text" id="cr_txt" placeholder="Find by: no.polisi, kode asset, merk, tipe, jenis, status"; onclick="this.value='';"<?php echo $_REQUEST['cr_txt'];?>" size="45" maxlength="100" />
	</label>
      <label>
      <input name="Search" type="submit" id="Search" value="Search" />
      </label>
    </form>
	<?php
	if(isset($_POST['Search'])) {
		echo "<script>window.location=\"index.php?component=masterkendaraan&cr_kel=".$_POST[cr_kel]."&cr_kel1=".$_REQUEST[cr_kel1]."&cr_txt=".str_replace(" ","+",$_REQUEST[cr_txt])."\";</script>";
	}
	?></td>
  </tr>
  <tr>
    <th width="125"><strong>No. Polisi </strong></th>
    <th width="80">Merk</th>
    <th width="231">Tipe &amp; Tahun </th>
    <th width="135">Jenis</th>
    <th width="165">Cabang</th>
    <th width="71">Area </th>
    <th width="165"><strong>Unit</strong></th>
    <th width="140"><strong> Stok Unit</strong></th>
    <th width="213" align="center" class="hide"><strong>Aksi</strong></th>
  </tr>
  <?php if($totalRows_data > 0) { ?>
  <?php do { ?>
    <tr valign="top" bgcolor="#FFFFFF">
      <td><a href="javascript:void(0);" title="Lihat Foto" onclick="viewImg('<?php echo $row_data['nopolisi'];?>');"><img src="images/car.png" width="16" hspace="5" border="0" /></a><span id="popUp<?php echo $row_data['nopolisi']; ?>" style="display:none;text-align:right;"><br />
      <a href="include/upload_image.php?id=<?php echo $row_data['nopolisi'];?>" onclick="NewWindow(this.href,'name','500','360','yes');return false" title="Upload Photo"><img src="<?php $p_img = "photo_mobil/".$row_data['nopolisi'].".png";if(file_exists($p_img)) { echo $p_img; } else{ ?>images/car.png<?php } ?>" border="0" id="img_<?php echo $row_data['nopolisi'];?>"/></a></span><?php echo $row_data['nopolisi']; ?></td>
      
      <td><?php echo $row_data['merk']; ?></td>
      <td><?php echo $row_data['tipekendaraan']; ?> - <?php echo $row_data['tahun']; ?></td>
      <td><?php echo $row_data['jeniskendaraan']; ?></td>
      <td><?php echo $row_data['gudang']; ?></td>
      <td><?php echo $row_data['area']; ?></td>
      <td><?php echo $row_data['status']; ?> - <?php echo $row_data['statusunit']; ?></td>
      <td><?php echo $row_data['ketersediaan']; ?></td>
      <td align="center" class="hide"><?php if(strstr($_SESSION[akses],"MS-8")) { ?>
	  <a href="#" onclick="NewWindow('index.php?component=masterkendaraan&task=view&id=<?php echo $row_data['id']; ?>&open=window','name','1250','550','yes');return false"><img src="images/view.gif" width="16" height="16" border="0" /><small>View</small></a><?php } ?>
	  <?php if(strstr($_SESSION[akses],"MS-8-2")) { ?><a href="index.php?component=masterkendaraan&amp;task=edit&amp;id=<?php echo $row_data['id']; ?>"><img src="images/edit_.png" border="0"/></a><?php } ?> 
	  <?php if(strstr($_SESSION[akses],"MS-8-3")) { ?><a href="index.php?component=masterkendaraan&amp;task=delete&amp;id=<?php echo $row_data['id']; ?>" class="ask"><img src="images/delete_.png" border="0" /></a><?php } ?></td>
    </tr>
    <?php } while ($row_data = mysql_fetch_assoc($data)); ?>
	<?php if($totalRows_data > $maxRows_data) { ?>
    <tr class="hide">
      <td colspan="9"><table border="0" align="left" width="50%">
          <tr>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, 0, $queryString_data); ?>"><img src="First.gif" border=0 ></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, max(0, $pageNum_data - 1), $queryString_data); ?>"><img src="Previous.gif" border=0></a>
                <?php } // Show if not first page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, min($totalPages_data, $pageNum_data + 1), $queryString_data); ?>"><img src="Next.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
            <td width="3%" align="center"><?php if ($pageNum_data < $totalPages_data) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_data=%d%s", $currentPage, $totalPages_data, $queryString_data); ?>"><img src="Last.gif" border=0></a>
                <?php } // Show if not last page ?>            </td>
			<td width="88%">Halaman : <?php echo $pageNum_data+1; ?>, Ditampilkan <?php echo $totalRow_data; ?> dari <?php echo $totalRows_data; ?> total data</td>
          </tr>
        </table></td>
    </tr>
	<?php } ?>
   <?php }else{ ?>
  <tr bgcolor="#FFFFFF">
    <td colspan="9">Data tidak ada !!! </td>
  </tr>
  <?php } ?>
  <tr class="hide">
    <td colspan="9"><?php if(strstr($_SESSION[akses],"MS-8-1")) { ?><a href="index.php?component=masterkendaraan&task=add"><img src="images/add_new.png" border="0" /></a><?php } ?></td>
  </tr>
</table>


<?php
mysql_free_result($data);

mysql_free_result($l_kel);
?>
